//最新代码，以 https://gitee.com/yiliangwu880/CppProto.git 项目为准。
//消息分发，参考用

#pragma once
#include "ProtoMarco.h"

#if 0

//管理消息派发
class ProtoDispatch : public Singleton<ProtoDispatch>
{
	using SvrFun = std::function<void(const proto::Session& con, const std::string& data)>;
	std::unordered_map<uint32_t, SvrFun> m_id2MsgData; //proto 消息ID 2 回调

public:
	//注册 proto消息回调
	template<class MsgType>
	void RegMsgHandler(uint32 cmdId, void(*fun)(const proto::Session& con, const MsgType& msg));
	void Dispatch(const proto::Session& con, uint32 cmdId, const std::string& data)
	{
		auto it = m_id2MsgData.find(cmdId);
		if (it == m_id2MsgData.end())
		{
			L_ERROR("unknow cmdId %d", cmdId);
			return;
		}
		SvrFun svrFun = it->second;
		svrFun(con, data);
	}

};


template<class MsgType>
void ProtoDispatch::RegMsgHandler(uint32 cmdId, void(*fun)(const proto::Session& con, const MsgType&))
{
	if (m_id2MsgData.find(cmdId) != m_id2MsgData.end())
	{
		L_ERROR("repeated reg. cmd=%d", cmdId);
		return;
	}
	SvrFun svrFun = [cmdId, fun](const proto::Session& con, const std::string& data)
	{
		MsgType msg;
		if (!msg.ParseFromString(data))
		{
			L_ERROR("ParseFromString fail, cmdId=%d", cmdId);
			return;
		}
		fun(con, msg);
	};
	m_id2MsgData[cmdId] = svrFun;
}



#endif // 0


